import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author wangj881
 * @date 2024-11-28 15:42
 */
public class L77_2 {

    public static void main(String[] args) {
        List<List<Integer>> combine = new Solution().combine(4, 2);
        for (List<Integer> integers : combine) {
            System.out.println(integers.toString());
        }

    }


    static class Solution {
        List<List<Integer>> result = new ArrayList<>();
        LinkedList<Integer> node = new LinkedList<>();

        public List<List<Integer>> combine(int n, int k) {
            int startIndex = 1;
            backTracking(n, k, startIndex);
            return result;
        }

        private void backTracking(int n, int k, int startIndex) {
            if (node.size() == k) {
                result.add(new ArrayList<>(node));
                return;
            }
            for (int i = startIndex; i <= n; i++) {
                node.add(i);
                backTracking(n, k, i + 1);
                node.removeLast();
            }
        }
    }

}
